<% layout('/layouts/default.html', {title: '预约管理', libs: ['dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header">
			<div class="box-title">
				<i class="fa icon-trophy"></i> ${text('预约管理')}
			</div>
			<div class="box-tools pull-right">
				<a href="#" class="btn btn-default" id="btnSearch" title="${text('查询')}"><i class="fa fa-filter"></i> ${text('查询')}</a>
				<input id="btnToday" class="btn btn-inverse" type="button" value="今日预约"/>
				<input id="btnSubmit" class="btn btn-inverse" type="button" value="今日提交"/>
				<input id="btnConfirm" class="btn btn-inverse" type="button" value="今日确认"/>
				<input id="btnExport" class="btn btn-inverse" type="button" value="导出"/>
				<% if(hasPermi('fl:appointment:edit')){ %>
					<a href="${ctx}/fl/appointment/form" class="btn btn-default btnTool" title="${text('新增预约')}"><i class="fa fa-plus"></i> ${text('新增')}</a>
					<input id="btnImport" class="btn btn-inverse" type="button" value="导入"/>
					<input id="btnFinish" class="btn btn-inverse" type="button" value="批量服务完成"/>
				<% } %>
			</div>
		</div>
		<div class="box-body">
			<#form:form id="searchForm" model="${appointment}" action="${ctx}/fl/appointment/listData" method="post" class="form-inline hide"
					data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
				<div class="form-group">
					<label class="control-label">${text('诊所')}：</label>
					<div class="control-inline width-120">
						<#form:select path="clinic.id" items="${clinicList}" itemLabel="name" itemValue="id" blankOption="true" class="form-control "/>
					</div>
				</div>
				<div class="form-group">
					<label class="control-label">${text('科室')}：</label>
					<div class="control-inline width-120">
						<#form:select path="department" dictType="fl_department" blankOption="true" class="form-control"/>
					</div>
				</div>
				<div class="form-group">
					<label class="control-label">${text('预约人手机')}：</label>
					<div class="control-inline width-100">
						<#form:input path="leaguer.cellphone" class="form-control"/>
					</div>
				</div>
				<div class="form-group">
					<label class="control-label">${text('预约人姓名')}：</label>
					<div class="control-inline width-100">
						<#form:input path="leaguer.name" class="form-control"/>
					</div>
				</div>
				<div class="form-group">
					<label class="control-label">${text('预约日期')}：</label>
					<div class="control-inline width-100">
						<#form:input path="scheduledTime" readonly="true" maxlength="20" class="form-control Wdate"
									dataFormat="date" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:true});"/>
					</div>
				</div>
				<div class="form-group">
					<label class="control-label">${text('提交日期')}：</label>
					<div class="control-inline width-100">
						<#form:input path="createDate" readonly="true" maxlength="20" class="form-control Wdate"
									dataFormat="date" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:true});"/>
					</div>
				</div>
				<div class="form-group">
					<label class="control-label">${text('确认日期')}：</label>
					<div class="control-inline width-100">
						<#form:input path="time" readonly="true" maxlength="20" class="form-control Wdate"
									dataFormat="date" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:true});"/>
					</div>
				</div>
				<div class="form-group">
					<label class="control-label">${text('预约状态')}：</label>
					<div class="control-inline width-120">
						<#form:select path="appointmentStatus" dictType="fl_appointment_status" blankOption="true" class="form-control"/>
					</div>
				</div>
				<div class="form-group">
					<button type="submit" class="btn btn-primary btn-sm" onclick="$('#pageNo').val(1);$('#searchForm').attr('action','${ctx}/fl/appointment/listData')">${text('查询')}</button>
					<button type="reset" class="btn btn-default btn-sm">${text('重置')}</button>
				</div>
			</#form:form>			
			<table id="dataGrid"></table>
			<div id="dataGridPage"></div>
		</div>
	</div>
</div>
<% } %>
<script>

Date.prototype.format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
	
$('#btnToday').click(function(){
	$('#pageNo').val(1);
	document.getElementById("searchForm").reset();
	$('#searchForm').attr('action','${ctx}/fl/appointment/listData');
	var now = new Date().format("yyyy-MM-dd");
	$("#scheduledTime").val(now);
	//$("#appointmentStatus").val('3').change();
	$('#searchForm').submit();
});

$('#btnSubmit').click(function(){
	$('#pageNo').val(1);
	document.getElementById("searchForm").reset();
	$('#searchForm').attr('action','${ctx}/fl/appointment/listData');
	var now = new Date().format("yyyy-MM-dd");
	$("#createDate").val(now);
	$('#searchForm').submit();
});

$('#btnConfirm').click(function(){
	$('#pageNo').val(1);
	document.getElementById("searchForm").reset();
	$('#searchForm').attr('action','${ctx}/fl/appointment/listData');
	var now = new Date().format("yyyy-MM-dd");
	$("#time").val(now);
	$('#searchForm').submit();
});

$('#btnFinish').click(function(){
	var idElements = document.getElementsByName("id");
	var ids = '';
	$.each(idElements, function (idx, item) {
		if(item.checked){
			ids += item.value + ',';
		}
	});
	if(ids.length==0){
		js.showMessage("至少需要选择一个需要完成服务的预约！","","warning");
		return false;
	}	
	js.confirm("确认要完成这些预约服务吗？", '${ctx}/fl/appointment/mutiFinish?ids=' + ids, function(data){if(data.result){js.showMessage(data.message);}else{js.showErrorMessage(data.message);};$('#searchForm').submit();});

});

$('#btnExport').click(function(){
	js.ajaxSubmitForm($('#searchForm'), {
		url:'${ctx}/fl/appointment/export',
		downloadFile:true
	});
});

$('#btnImport').click(function(){
	js.layer.open({
		type: 1,
		area: ['400px'],
		title: '导入预约数据',
		resize: false,
		scrollbar: true,
		content: js.template('importTpl'),
		success: function(layero, index){
			layero.find('input[type="checkbox"]').iCheck();
		},
		btn: ['<i class="fa fa-check"></i> 导入',
			'<i class="fa fa-remove"></i> 取消'],
		btn1: function(index, layero){
			var form = {
				inputForm: layero.find('#inputForm'),
				file: layero.find('#file').val()
			};
		    if (form.file == '' || (!js.endWith(form.file, '.xls') && !js.endWith(form.file, '.xlsx'))){
		    	js.showMessage("${text('文件不正确，请选择后缀为 “xls”或“xlsx”的文件。')}", null, 'warning');
		        return false;
		    }
			js.ajaxSubmitForm(form.inputForm, function(data){
				js.showMessage(data.message);
				if(data.result == Global.TRUE){
					js.layer.closeAll();
				}
				page();
			}, "json");
			return true;
		}
	});
});

// 初始化DataGrid对象
$('#dataGrid').dataGrid({
	searchForm: $("#searchForm"),
	columnModel: [
		{header:'${text('选择')}', name:'id', width:50, align:"center", sortable:false, formatter: function(val, obj, row, act){
			return row.appointmentStatus == '3'?'<input id="id" name="id" type="checkbox" value='+row.id+'>':'';
		}},
		{header:'${text('预约编号')}', name:'no', width:200, align:"center", frozen:true, formatter: function(val, obj, row, act){
			return '<a href="${ctx}/fl/appointment/view?id='+row.id+'" class="btnList" data-title="${text('查看预约')}">'+(val||row.id)+'</a>';
		}},
		{header:'${text('预约人手机')}', name:'leaguer.cellphone', sortable:false, width:200, align:"center"},
		{header:'${text('预约人姓名')}', name:'leaguer.name',  sortable:false, width:150, align:"center"},
		{header:'${text('地区')}', name:'area.areaName', sortable:false, width:150, align:"center"},
		{header:'${text('诊所')}', name:'clinic.name', sortable:false,  width:150, align:"center"},
		{header:'${text('预约日期')}', name:'scheduledTime', index:'a.scheduled_time', width:150, align:"center"},
		{header:'${text('时间片')}', name:'timePeriod', index:'a.time_period', width:150, align:"center"},
		{header:'${text('提交日期')}', name:'createDate', index:'a.create_date', width:150, align:"center"},
		{header:'${text('确认时间')}', name:'time', index:'a.time', width:150, align:"center"},
		{header:'${text('科室')}', name:'department', index:'a.department', width:150, align:"center", formatter: function(val, obj, row, act){
			return js.getDictLabel(${@DictUtils.getDictListJson('fl_department')}, val, '未知', true);
		}},
		{header:'${text('预约状态')}', name:'appointmentStatus', index:'a.appointment_status', width:150, align:"center", formatter: function(val, obj, row, act){
			return js.getDictLabel(${@DictUtils.getDictListJson('fl_appointment_status')}, val, '未知', true);
		}},
		{header:'${text('备注信息')}', name:'remarks', index:'a.remarks', width:200, align:"left"},

		{header:'${text('操作')}', name:'actions', width:240, sortable:false, title:false, formatter: function(val, obj, row, act){
			var actions = [];
			<% if(hasPermi('fl:appointment:edit')){ %>
				if (row.appointmentStatus == '1' || row.appointmentStatus == '2' ){
					actions.push('<a href="${ctx}/fl/appointment/form?id='+row.id+'" class="btnList" title="${text('修改预约')}"><i class="fa fa-pencil"></i></a>&nbsp;');
				}
				if (row.appointmentStatus == '1' || row.appointmentStatus == '2' ){
					actions.push('<a href="${ctx}/fl/appointment/confirm?id='+row.id+'" class="btnList" title="${text('确认预约')}"><i class="fa fa-check"></i></a>&nbsp;');
				}
				if (row.appointmentStatus == '3' ){
					actions.push('<a href="${ctx}/fl/appointment/finish?id='+row.id+'" class="btnList" title="${text('完成服务')}" data-confirm="${text('确认要完成该预约服务吗？')}"><i class="fa fa-flag"></i></a>&nbsp;');
				}
				actions.push('<a href="${ctx}/fl/appointment/view?id='+row.id+'" class="btnList" title="${text('查看预约')}"><i class="fa fa-eye"></i></a>&nbsp;');
				actions.push('<a href="${ctx}/fl/appointment/cancel?id='+row.id+'" class="btnList" title="${text('取消预约')}"><i class="fa fa-eraser"></i></a>&nbsp;');
			<% } %>
			return actions.join('');
		}}
	],
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		
	}
});
</script>

<script id="importTpl" type="text/template">//<!--
<form id="inputForm" action="${ctx}/fl/appointment/import" method="post" enctype="multipart/form-data"
	class="form-horizontal mt20 mb10" style="overflow:auto;max-height:200px;">
	<div class="row">
		<div class="col-xs-12 col-xs-offset-1">
			<input type="file" id="file" name="file" class="form-file"/>
			<div class="mt10 pt5">
				<#form:checkbox name="updateSupport" label="${text('是否更新已经存在的预约数据')}" class="form-control"
					title="${text('如果预约已经存在，更新这条数据。')}"/> &nbsp;
				<a href="${ctx}/fl/appointment/importTemplate" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> ${text('下载模板')}</a>
			</div>
			<font color="red" class="pull-left mt10">
				${text('提示：仅允许导入“xls”或“xlsx”格式文件！')}
			</font>
		</div>
	</div>
</form>
//--></script>